package com.robam.gulimall.product.service.impl;

import com.robam.gulimall.product.entity.CategoryBrandRelationEntity;
import com.robam.gulimall.product.service.CategoryBrandRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.robam.common.utils.PageUtils;
import com.robam.common.utils.Query;

import com.robam.gulimall.product.dao.BrandDao;
import com.robam.gulimall.product.entity.BrandEntity;
import com.robam.gulimall.product.service.BrandService;
import org.springframework.transaction.annotation.Transactional;


@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {

    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<BrandEntity> page = this.page(
                new Query<BrandEntity>().getPage(params),
                new QueryWrapper<BrandEntity>()
        );

        return new PageUtils(page);
    }

    // 删除品牌一并删除品牌和分类的关联数据
    @Override
    @Transactional
    public void deleteBrands(Long[] brandIds) {
        // 删除品牌和分类的关联数据
        QueryWrapper<CategoryBrandRelationEntity> wrapper = new QueryWrapper<>();
        wrapper.in("brand_id", brandIds);
        if(categoryBrandRelationService.count(wrapper) > 0) {
            categoryBrandRelationService.remove(wrapper);
        }
        // 删除品牌资料
        this.baseMapper.deleteBatchIds(Arrays.asList(brandIds));
    }

}